package com.program.utils;

import com.program.eneity.Airplane;

/**
 * @program: AirportSystem
 * @description: 循环队列
 * @author: 19071027
 * @create: 2021-10-11 14:56
 **/
public class ArrayQueue implements Queue {
    private int front=0;
    private int tail=0;
    private int MAX_SIZE = 4;
    int size=0;
    private Airplane[] arrayQueue = new Airplane[MAX_SIZE];

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public boolean add(Object o) {
        if (isFull()) {
            return false;
        }
        arrayQueue[tail] = (Airplane) o;
        tail = (tail + 1) % MAX_SIZE;
        size++;

        //根据燃油容量进行优先级排序 实现：冒泡排序
        for (int i = front; i < size; i++) {
            for (int j = front; j < size - i - 1; j++) {
                if (arrayQueue[j + 1].getFuel() < arrayQueue[j].getFuel()) {
                    Airplane temp = arrayQueue[j];
                    arrayQueue[j] = arrayQueue[j + 1];
                    arrayQueue[j + 1] = temp;
                }
            }
        }
        return true;
    }

    @Override
    public Airplane remove() {
        if (isEmpty()) {
            return null;
        }
        Airplane airplane = arrayQueue[front];
        front = (front + 1) % MAX_SIZE;
        size--;
        return airplane;
    }

    @Override
    public Object peek() {
        return arrayQueue[front];
    }

    public boolean isFull() {
        return size==MAX_SIZE;
    }
}
